﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace RepairTools
{
    public partial class Repair : Form
    {
        public Repair()
        {
            InitializeComponent();
        }
         
        private static string pathdb = Application.StartupPath + @"\MDB\smallerp.mdb";
        private static string backupdb = Application.StartupPath + @"\MDB\backup.mdb";
        private string connStr = @"Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + pathdb + ";Jet OLEDB:Database Password=123";
        private string MyConnstr = @"Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + pathdb + ";Jet OLEDB:Database Password=123";
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            backgroundWorker1.ReportProgress(1, "5");
            backgroundWorker1.ReportProgress(2, "开始准备备份");
            string path2 = pathdb.Substring(0, pathdb.LastIndexOf('\\')) + "\\smallerp.mdb";
            string path3 = pathdb.Substring(0, pathdb.LastIndexOf('\\')) +"\\backupTemp.mdb";
            try
            {
                File.Copy(path2, path3, true);
            }
            catch
            {
                backgroundWorker1.ReportProgress(-1, "数据正在被使用，不能完成备份，请先退出合成照片服务");
                return;
            }
            backgroundWorker1.ReportProgress(2, "备份数据成功");

            backgroundWorker1.ReportProgress(1, "30");
          
            string strS = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath.Trim() + "\\" + "smallerp.mdb"+";Jet OLEDB:Database Password=123";
            string strT = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath.Trim() + "\\" + "temp.mdb" + ";Jet OLEDB:Database Password=123";
            backgroundWorker1.ReportProgress(2, "正在修复数据");
            backgroundWorker1.ReportProgress(1, "50");

            JRO.JetEngine jet = new JRO.JetEngineClass();
            jet.CompactDatabase(strS, strT);
            backgroundWorker1.ReportProgress(2, "正在修复数据");
            backgroundWorker1.ReportProgress(1, "80");

            File.Delete(pathdb);

            File.Copy(Application.StartupPath.Trim() + "\\" + "temp.mdb", pathdb);
            File.Delete(Application.StartupPath.Trim() + "\\" + "temp.mdb");
            backgroundWorker1.ReportProgress(2, "修复数据命令已成功！");
            backgroundWorker1.ReportProgress(1, "100");
 
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            if (e.ProgressPercentage == -1||e.ProgressPercentage==2)
            {
                label2.Text = (string)e.UserState;
            }
            else if (e.ProgressPercentage == 1)
            {
                progressBar1.Value = Convert.ToInt16(e.UserState);
            }
            else 
            {
                progressBar1.Value = 100;
            }
                
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            progressBar1.Value = 100;
        }

        private void Repair_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

            backgroundWorker1.RunWorkerAsync();
        }

        private void buttonReStore_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            try
            {
                progressBar1.Value = 10;

                if (MessageBox.Show("还原数据将清空现有拼版数据，并还原为上次备份数据", "是否还原数据", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {

                    File.Copy(Application.StartupPath.Trim() + "\\" + "backup.mdb", pathdb, true);
                }
                progressBar1.Value = 100;
                label2.Text = "数据还原命令已成功！！";
            }
            catch
            {
            }
            finally
            {
                this.Cursor = Cursors.Default;
 
            }
            
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            progressBar1.Value = 10;
            try
            {
                this.Cursor = Cursors.WaitCursor;
                File.Copy(pathdb, Application.StartupPath.Trim() + "\\" + "backup.mdb", true);

                progressBar1.Value = 100;
                label2.Text = "数据备份命令已成功！！";
            }
            catch
            {

            }
            finally
            {
                this.Cursor = Cursors.Default;
 
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否取消修复", "确定", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                string path2 = pathdb.Substring(0, pathdb.LastIndexOf('\\')) + "\\smallerp.mdb";
                string path3 = pathdb.Substring(0, pathdb.LastIndexOf('\\')) + "\\backupTemp.mdb";
                try
                {
                    File.Copy(path3, path2, true);
                }
                catch
                {
                    MessageBox.Show("数据正在被使用，不能完成'取消修复'");
                    //return;
                }
                label2.Text = "取消修复命令已成功！";
            }
        }
        
    }
}
